/*****************************************************************
                        First come first serve
PURPOSE: This is the algorithm for first-come-first-serve CPU scheduling. It 
inherits data and functionalities from base class Scheduler.
*****************************************************************/
import java.util.ArrayList;

public class FCFS extends Scheduler implements Runnable {

   /*--------------------------------------------------------
                        Constructor
   PURPOSE:  To ask help from views, set up data, and begin simulation
   PARAMTERS: references the input queue, stats and anime view. gets
   value of starting clock time.
   --------------------------------------------------------*/
   public FCFS(ArrayList<Process> q, statsFrame s, animeFrame a, inputFrame i, int c) {
      super(q,s,a,i,c);
    
      thread = new Thread(this,"FCFS");
      thread.start();
   } // constructor

   /*--------------------------------------------------------
                        Thread Run
   PURPOSE:  To run light-weight thread simulation
   --------------------------------------------------------*/
   public void run() {
      int all=queue.size();     

      do {
         clock++;
         T = processready(clock);
         if (T != null) {
            readyQueue.add(T);
            queue.remove(T);
            an.upstatus("Time "+T.getArrival()+":Process "+T.getName()+" ready.");
            try { Thread.sleep(1000); } catch (InterruptedException e) {};
         } // put in ready queue
         if (idle) {
            if (readyQueue.size()==0)
               continue;
            idle = false;
            P = (Process)readyQueue.get(0);
            readyQueue.remove(P);            
         } // put in run state
         P.servicing();
         an.drawbar(P,clock);
         an.upstatus("Time "+clock+":Serving process "+P.getName()+".");
         try { Thread.sleep(1000); } catch (InterruptedException ex) {};
         if (P.getTminus()==0) {
            an.upstatus("Time "+(clock+1)+":Process "+P.getName()+" done.");
            try { Thread.sleep(1000); } catch (InterruptedException e) {};
            P.report(clock+1); // anticipate completion
            finishQueue.add(P);
            idle = true;
         } // put in finish queue
      } while (finishQueue.size()<all);
      an.upstatus("Algorithm finished.");
      st.report(finishQueue,"First Come First Serve");
      in.resetGUI();
   } // run thread

} // FCFS class
